package crawler.chuanqi;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zql.entity.AgencyEntity;

import dao.AgencyDao;
import util.HttpConnectionGet;
import util.HttpConnectionPost;
import util.MybatisTool;
import util.TianYanCha;

public class GuangQiChengYongCheCrawler {
	public List<AgencyEntity> getAgency() {
		List<AgencyEntity> list = new ArrayList<AgencyEntity>();
		try {
			String getPro = "http://trumpchi.gacmotor.com/index.php/online_sales";
			Document proDoc = Jsoup.connect(getPro).get();
			Element proSelect = proDoc.getElementById("pid");
			Elements proOptions = proSelect.getElementsByTag("option");
			for(int i=1;i<proOptions.size();i++) {
				String proName = proOptions.get(i).text();
				String proNum = proOptions.get(i).attr("value");
				//获取所有的城市
				String getCity = "http://trumpchi.gacmotor.com/index.php/online_sales/ajax_get_city";
				//String paramCity = "pid="+proNum;
				//String cityText = HttpConnectionPost.getJson(getCity, paramCity);
				//System.out.println(cityText);
				String cityText = HttpConnectionGet.getJson(getCity);
				JSONArray cityArray = JSON.parseArray(cityText);
				for(int j=0;j<cityArray.size();j++) {
					//获取这个城市的省份id
					String proID = cityArray.getJSONObject(j).getString("parentid");
					if(proNum.equals(proID)) {
						String cityNum = cityArray.getJSONObject(j).getString("id");
						String cityName = cityArray.getJSONObject(j).getString("name");
						
						String getDealer = "http://trumpchi.gacmotor.com/index.php/online_sales/ajax_get_sellpoint";
						String paramDealer = "pid="+proNum+"&cid="+cityNum;
						String dealerText = HttpConnectionPost.getJson(getDealer, paramDealer);
						JSONArray dealerArray = JSON.parseArray(dealerText);
						for(int k=0;k<dealerArray.size();k++) {
							JSONObject dealer = dealerArray.getJSONObject(k);
							String address = dealer.getString("address");
							String sellTell = dealer.getString("call");
							String name = dealer.getString("name");
							String lng = dealer.getString("longitude");
							String lat = dealer.getString("latitude");
							List<String> shareholder = TianYanCha.getShareholder(name);
							String sControllingShareholder = shareholder.get(0);
							String sOtherShareholders = shareholder.get(1);
							
							
							AgencyEntity agency = new AgencyEntity();
							agency.setdCloseDate(null);
							agency.setdOpeningDate(null);
							agency.setdUpdateTime(new Timestamp(System.currentTimeMillis()));
							agency.setnBrandID(-1);
							agency.setsBrand("传祺");

							agency.setnDealerIDWeb(-1);
							agency.setnManufacturerID(-1);
							agency.setsManufacturer("广汽乘用车");

							agency.setnState(1);
							agency.setsAddress(address);
							agency.setsCity(cityName);
							agency.setsCustomerServiceCall(null);
							agency.setsDealerName(name);
							agency.setsDealerType(null);
							agency.setsProvince(proName);
							agency.setsSaleCall(sellTell);
							agency.setsLongitude(lng);
							agency.setsLatitude(lat);
							agency.setsControllingShareholder(sControllingShareholder);
							agency.setsOtherShareholders(sOtherShareholders);
							list.add(agency);
						}
					}
					
				}
			}
		} catch(Exception e) {
			throw new RuntimeException("爬虫异常", e);
		}
		return list;
	}
	public static void main(String[] args) {
		GuangQiChengYongCheCrawler crawler = new GuangQiChengYongCheCrawler();
		System.out.println("爬虫开始...");
		AgencyDao dao = new AgencyDao();
		List<AgencyEntity> agencys = crawler.getAgency();
		System.out.println("抓取完毕,正在存库");
		for(AgencyEntity agency : agencys) {
			MybatisTool.save(agency);
		}
		MybatisTool.close();
		System.out.println("请查看数据库");
		
		
	}

}
